(function () {
    window.visibly = {
        b: null,
        q: document,
        p: undefined,
        prefixes: ["webkit", "ms"],
        props: ["VisibilityState", "visibilitychange", "Hidden"],
        m: ["focus", "blur"],
        visibleCallbacks: [],
        hiddenCallbacks: [],
        _callbacks: [],
        onVisible: function (a) {
            this.visibleCallbacks.push(a)
        },
        onHidden: function (a) {
            this.hiddenCallbacks.push(a)
        },
        isSupported: function () {
            return (this._supports(0) || this._supports(1))
        },
        _supports: function (a) {
            return ((this.prefixes[a] + this.props[2]) in this.q)
        },
        runCallbacks: function (a) {
            if (a) {
                this._callbacks = (a == 1) ? this.visibleCallbacks : this.hiddenCallbacks;
                for (var b = 0; b < this._callbacks.length; b++) {
                    this._callbacks[b]()
                }
            }
        },
        _visible: function () {
            window.visibly.runCallbacks(1)
        },
        _hidden: function () {
            window.visibly.runCallbacks(2)
        },
        _nativeSwitch: function () {
            ((this.q[this.b + this.props[2]]) === true) ? this._hidden(): this._visible()
        },
        listen: function () {
            try {
                if (!(this.isSupported())) {
                    if (document.addEventListener) {
                        window.addEventListener(this.m[0], this._visible, 1);
                        window.addEventListener(this.m[1], this._hidden, 1)
                    } else {
                        this.q.attachEvent("onfocusin", this._visible);
                        this.q.attachEvent("onfocusout", this._hidden)
                    }
                } else {
                    this.b = (this._supports(0) == this.p) ? this.prefixes[1] : this.prefixes[0];
                    this.q.addEventListener(this.b + this.props[1], function () {
                        window.visibly._nativeSwitch.apply(window.visibly, arguments)
                    }, 1)
                }
            } catch (a) {}
        },
        init: function () {
            this.listen()
        }
    };
    this.visibly.init()
})();
(function () {
    var doc = document;
    var loc = window.location;
    var navigator = window.navigator;
    var screen = window.screen;
    var userAgent = navigator.userAgent;
    var isIE = /MSIE|Edge|Trident/i.test(userAgent);
    var isApp = /^\S+(\sSonic\/[\d.]+)?$/.test(userAgent);
    var LANCE_API = "//shrek.6.cn/live.6.cn/e6u2/rp.php";
    var LANCE_CALLBACK = "_jiadelee_";
    var ZHANG_ZHI_API = "//sclick.6rooms.com/s.html";
    var ZHANG_ZHI_QUERY_NAME = "_tra_";
    var ZHANG_ZHI_COOKIE_NAME = "_tracing";
    var ZHANG_ZHI_COOKIE_DOMAIN = isIE ? "" : "6.cn";
    if (isApp) {
        return
    }
    var via = window.via = window.via || {};
    via._h = via._h || [];
    via.uuid = "";
    via.ready = function (handler) {
        this._h.push(handler);
        if (this.uuid) {
            this.triggerReady()
        }
    };
    via.triggerReady = function () {
        var handlerList = this._h;
        var handler;
        while (handler = handlerList.shift()) {
            handler.apply(null, [this.uuid, identifyPageName()])
        }
    };
    var cookie = {
        _createCookieString: function (name, value, options) {
            var text = name + "=" + encodeURIComponent(value);
            var options = options || {};
            var expires = options.expires;
            var domain = options.domain;
            var path = options.path || "/";
            if (expires instanceof Date) {
                text += ";expires=" + expires.toUTCString()
            }
            if (domain) {
                text += ";domain=" + domain
            }
            if (path) {
                text += ";path=" + path
            }
            if (options.secure === true) {
                text += ";secure"
            }
            return text
        },
        _parseCookieString: function (text) {
            if (!text) {
                return null
            }
            var cookies = {};
            var cookiePark = text.split(/;\s/g);
            var cookieNameValue;
            for (var i = 0; i < cookiePark.length; i++) {
                cookieNameValue = cookiePark[i].split("=");
                cookies[cookieNameValue[0]] = decodeURIComponent(cookieNameValue[1])
            }
            return cookies
        },
        get: function (name) {
            var cookies = this._parseCookieString(doc.cookie);
            if (cookies && cookies[name] !== undefined) {
                return cookies[name]
            }
            return null
        },
        set: function (name, value, options) {
            var text = this._createCookieString(name, value, options);
            doc.cookie = text
        },
        remove: function (name, options) {
            options = options || {};
            options.expires = new Date(0);
            doc.cookie = this._createCookieString(name, "", options)
        }
    };
    var getFlashVersion = function () {
        var result = "-";
        if (navigator.plugins && navigator.plugins.length) {
            for (var c = 0; c < navigator.plugins.length; c++) {
                if (navigator.plugins[c].name.indexOf("Shockwave Flash") > -1) {
                    result = navigator.plugins[c].description.split("Shockwave Flash ")[1];
                    break
                }
            }
        } else {
            if (window.ActiveXObject) {
                for (var c = 10; c >= 2; c--) {
                    try {
                        if (eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash." + c + "');")) {
                            result = c + ".0";
                            break
                        }
                    } catch (d) {}
                }
            }
        }
        return result
    };
    var stripProtocal = function (url) {
        return encodeURIComponent(url.replace(/^https?\:\/\//, ""))
    };
    var getUrlParam = function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var result = window.location.search.substr(1).match(reg);
        return result ? decodeURIComponent(result[2]) : null
    };
    var getUid = function () {
        var result = "0";
        var vinfo = cookie.get("_vinfo");
        var coin6 = cookie.get("_coin6");
        var ticket = cookie.get("ticket_v");
        if (ticket && vinfo && vinfo != "deleted" && coin6) {
            result = coin6.split(":")[0]
        }
        return result
    };
    var insertScript = function (url) {
        var script = document.createElement("script");
        var head = document.getElementsByTagName("head")[0];
        script.type = "text/javascript";
        script.src = url;
        head.insertBefore(script, head.firstChild)
    };
    var toLance = function () {
        var _KEY = {
                liaotian: "qpu9m2eykk",
                jiaoyou: "pxr4cee70a",
                redian: "lbw8gqtymm"
            },
            href = loc.href;
        for (var k in _KEY) {
            var _reg = new RegExp(k, "i");
            if (_reg.test(location.pathname)) {
                var arr = href.split("?"),
                    src = _KEY[k];
                href = arr[0] + "?src=" + src + (arr[1] ? "&" + arr[1] : "");
                break
            }
        }
        insertScript(LANCE_API + "?location=" + stripProtocal(href) + "&referer=" + stripProtocal(document.referrer || "") + "&browser=" + encodeURIComponent(userAgent.toLowerCase()) + "&flash=" + encodeURIComponent(getFlashVersion()) + "&msr=" + encodeURIComponent(screen.width + "," + screen.height) + "&uid=" + getUid() + "&pro=4&stamp=" + (new Date().getTime()))
    };
    var checkCrawler = function () {
        var reg = new RegExp("Baiduspider|Googlebot|\\sSlurp|iaskspider|YodaoBot|msnbot|\\sspider", "i");
        return reg.test(userAgent)
    };
    var identifyPageName = function () {
        var pathname = loc.pathname;
        var result = pathname;
        if (pathname == "/" || pathname == "/index4.html" || pathname == "/index2.shtml" || pathname == "/index4b.html" || pathname == "/index2b.shtml") {
            result = window.VIA_PAGEID || "index"
        } else {
            if (/^\/\d+$/.test(pathname)) {
                result = "room";
                if (window.page) {
                    if (page.tpl == "room_dating") {
                        result = "room_dating"
                    } else {
                        if (page.roomType == "voice") {
                            result = "room_voice"
                        }
                    }
                }
            } else {
                if (/^\/minivideo\/$/.test(pathname)) {
                    result = "mvideo"
                } else {
                    if (/^\/profile\/watchMini\.php$/.test(pathname)) {
                        result = "vplay"
                    } else {
                        if (/^\/f\/\d+$/.test(pathname)) {
                            result = "room_family"
                        } else {
                            if (/^\/f\/p\/\d+\.html$/.test(pathname)) {
                                result = "family_index"
                            } else {
                                if (/^\/video\/\d+\.html$/.test(pathname)) {
                                    result = "video_play"
                                } else {
                                    result = pathname
                                }
                            }
                        }
                    }
                }
            }
        }
        return result
    };
    var identifyPageId = function (pageName) {
        var url = loc.href;
        var pathname = loc.pathname;
        var reg = [];
        var result = "";
        if (pageName == "room" || pageName == "room_family" || pageName == "room_dating" || pageName == "room_voice") {
            result = (window.page && page.rid) || ""
        } else {
            if (pageName == "vplay") {
                result = (window.PAGE_ENV && PAGE_ENV.vid) ? PAGE_ENV.vid : ""
            } else {
                if (pageName == "family_index") {
                    result = (window.PAGE_ENV && PAGE_ENV.fuid) ? PAGE_ENV.fuid : ""
                } else {
                    if (pageName == "video_play") {
                        reg = pathname.match(/^\/video\/(\d+)\.html$/);
                        result = reg[1]
                    } else {
                        if (pageName.indexOf("coopgame/play.php") > -1) {
                            result = getUrlParam("gid") || 0
                        } else {
                            if (pageName.indexOf("redian/kwd.php") > -1) {
                                result = getUrlParam("id") || 0
                            } else {
                                result = ""
                            }
                        }
                    }
                }
            }
        }
        return result
    };
    var identifyClickTarget = function (target) {
        var event = "click";
        var mod = "noname";
        var url = "";
        var recid = window.ZHANG_ZHI_RECID || "";
        var parentAnchor = null;
        var topageid = "";
        var stat = "";
        var uid = "";
        var liveid = "";
        var recSrc = "";
        while (target && target.parentNode) {
            if (target.tagName.toLowerCase() == "a") {
                var href = target.getAttribute("href");
                if (href && href.indexOf("#") < 0) {
                    event = "in";
                    url = href;
                    parentAnchor = target;
                    var reg = href.match(/\/(\d+)\/?/) || href.match(/\?vid=(\d+)\/?/) || [];
                    topageid = reg[1] || ""
                }
            }
            if (target.getAttribute("data-recid")) {
                recid = target.getAttribute("data-recid")
            }
            if (target.getAttribute("data-stat")) {
                stat = target.getAttribute("data-stat")
            }
            var dataTracing;
            if ((dataTracing = target.getAttribute("data-tracing"))) {
                mod = dataTracing;
                target = null
            } else {
                target = target.parentNode
            }
        }
        stat = String(stat || "").split("|");
        uid = stat[0] || "";
        liveid = stat[1] || "";
        recSrc = stat[2] || "";
        return {
            event: event,
            mod: mod,
            url: url,
            recid: recid,
            topageid: topageid,
            liveid: liveid,
            recSrc: recSrc,
            parentAnchor: parentAnchor
        }
    };
    var makeTracingCode = function (pageName, mdouleId) {
        return pageName + "-" + mdouleId
    };
    var _serialize = function (obj) {
        var str = [];
        for (var p in obj) {
            if (obj.hasOwnProperty(p)) {
                str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]))
            }
        }
        return str.join("&")
    };
    var _toZhangZhi = function (event, mod, extraData) {
        var $ = jQuery;
        var resolution = screen.width + "/" + screen.height;
        var userInfo = (typeof pageMessage == "object" && pageMessage.user && pageMessage.user.uid) ? ('{"uid":' + pageMessage.user.uid + ',"coinrank":' + pageMessage.user.rich + ',"newcoinrank":' + pageMessage.user.new_rich + ',"wealthrank":' + pageMessage.user.star + "}") : "";
        var _rid = "";
        if (window.PAGE_ENV && PAGE_ENV.puid) {
            _rid = PAGE_ENV.puid
        } else {
            if (window.page && page.puid) {
                _rid = page.puid
            }
        }
        var params = {};
        var extraData = extraData || {};
        var url = extraData.url || "";
        var recid = extraData.recid || window.ZHANG_ZHI_RECID || "";
        $.extend(params, extraData, {
            event: event,
            module: mod || "noname",
            uuid: pageMessage._uuid || "",
            page: pageMessage._page || "",
            pageid: pageMessage._pageid || "",
            rid: _rid,
            data: userInfo,
            recid: recid,
            from_module: pageMessage._tracing || "",
            from_pageid: pageMessage._from_pageid || "",
            from_recid: pageMessage._from_recid || "",
            from_liveid: pageMessage._from_liveid || "",
            from_recSrc: pageMessage._from_recSrc || "",
            liveid: pageMessage._liveid || 0,
            watchid: pageMessage._watchid || "",
            reft: pageMessage._reft || "",
            platform: pageMessage._platform || "",
            ver: pageMessage._ver || ""
        });
        if (event == "in") {
            $.extend(params, {
                topageid: extraData.topageid || "",
                liveid: extraData.liveid,
                recSrc: extraData.recSrc,
                url: url
            })
        }
        if (event == "load") {
            $.extend(params, {
                resolution: resolution,
                url: url,
                reffer: document.referrer
            })
        }
        new Image().src = ZHANG_ZHI_API + "?" + _serialize(params);
        if (event == "in") {
            if (url && (url.indexOf("payshow.php") > -1 || url.indexOf("replacePayshow.php") > -1 || url.indexOf("paygame.php") > -1 || url.indexOf("ylPayShow.php") > -1)) {
                if (location.pathname.indexOf("payshow.php") > -1) {
                    return
                }
                cookie.set("pay_tracing", pageMessage._page + "|" + pageMessage._pageid + "|" + (pageMessage._tracing || "") + "|" + (mod || "") + "|" + (pageMessage._watchid || ""), {
                    domain: ZHANG_ZHI_COOKIE_DOMAIN
                })
            } else {
                cookie.set(ZHANG_ZHI_COOKIE_NAME, makeTracingCode(pageMessage._page, mod) + "|" + (pageMessage._pageid || "") + "|" + recid + "|" + (extraData.liveid || "") + "|" + (extraData.recSrc || ""), {
                    domain: ZHANG_ZHI_COOKIE_DOMAIN
                })
            }
        }
    };
    var establishZhangZhiAnalytic = function (uuid, reft) {
        if (typeof JSF == "object" && JSF.jq && JSF.login) {
            $LAB.script(JSF.jq, JSF.login).wait(function () {
                pageMessage._uuid = uuid || 0;
                pageMessage._reft = reft || 0;
                pageMessage._page = identifyPageName();
                pageMessage._pageid = identifyPageId(pageMessage._page);
                pageMessage._watchid = new Date().getTime();
                pageMessage._liveid = (window.page && page.liveid) || 0;
                pageMessage._platform = "web";
                pageMessage._ver = "1.5";
                pageMessage._isCrawler = checkCrawler();
                if (pageMessage._isCrawler) {
                    return
                }
                var $ = jQuery;
                var pageName = pageMessage._page;
                var loadModule = "";
                if (pageName == "room" || pageName == "vplay" || pageName == "room_dating" || pageName == "room_voice" || pageName.indexOf("coopgame/play.php") > -1) {
                    var count = 0;
                    var accumulator = 0;
                    var setTimeoutTime = 0;
                    var beatPaused = true;
                    var timer = null;
                    loadModule = "live";
                    if (pageName == "vplay") {
                        loadModule = "video"
                    } else {
                        if (pageName.indexOf("coopgame/play.php") > -1) {
                            loadModule = "play"
                        }
                    }
                    var getInterval = function () {
                        var result = 60;
                        if (count < 3) {
                            result = 5
                        } else {
                            if (count < 6) {
                                result = 10
                            } else {
                                if (count < 9) {
                                    result = 30
                                }
                            }
                        }
                        return result
                    };
                    var prepareNextBeat = function () {
                        cancelNextBeat();
                        setTimeoutTime = (new Date()).getTime();
                        timer = setTimeout(heartBeat, getInterval() * 1000)
                    };
                    var cancelNextBeat = function () {
                        clearTimeout(timer)
                    };
                    var startHeartBeat = function () {
                        if (beatPaused) {
                            prepareNextBeat();
                            beatPaused = false
                        }
                    };
                    var pauseHeartBeat = function () {
                        if (!beatPaused) {
                            cancelNextBeat();
                            reportHeartBeat();
                            beatPaused = true
                        }
                    };
                    var reportHeartBeat = function () {
                        var userInfo = (typeof pageMessage == "object" && pageMessage.user && pageMessage.user.uid) ? ('{"uid":' + pageMessage.user.uid + ',"coinrank":' + pageMessage.user.rich + ',"newcoinrank":' + pageMessage.user.new_rich + ',"wealthrank":' + pageMessage.user.star + "}") : "";
                        var now = (new Date()).getTime();
                        var durationFromLast = Math.round((Math.min(now - setTimeoutTime, 60 * 1000)) / 1000);
                        var lookType = 0;
                        var param = {};
                        accumulator += durationFromLast;
                        if (window.page && page.liveType / 1) {
                            lookType = page.liveVideo / 1 ? 2 : 1
                        }
                        var _rid = 0;
                        if (window.PAGE_ENV && PAGE_ENV.puid) {
                            _rid = PAGE_ENV.puid
                        } else {
                            if (window.page && page.puid) {
                                _rid = page.puid
                            }
                        }
                        param = {
                            tm: durationFromLast,
                            looktm: accumulator,
                            lookType: lookType,
                            event: "loop",
                            module: loadModule,
                            uuid: pageMessage._uuid || "",
                            page: pageMessage._page || "",
                            pageid: pageMessage._pageid || "",
                            rid: _rid,
                            data: userInfo,
                            from_module: pageMessage._tracing || "",
                            from_pageid: pageMessage._from_pageid || "",
                            from_recid: pageMessage._from_recid || "",
                            from_liveid: pageMessage._from_liveid || "",
                            from_recSrc: pageMessage._from_recSrc || "",
                            liveid: pageMessage._liveid || 0,
                            watchid: pageMessage._watchid || "",
                            reft: pageMessage._reft || "",
                            platform: pageMessage._platform || "",
                            ver: pageMessage._ver || ""
                        };
                        if (durationFromLast > 3) {
                            (new Image()).src = ZHANG_ZHI_API + "?" + _serialize(param)
                        }
                    };
                    var heartBeat = function () {
                        count++;
                        reportHeartBeat();
                        prepareNextBeat()
                    };
                    var mouseoverHandler = function (event) {
                        startHeartBeat();
                        $(document).off("mouseover", mouseoverHandler)
                    };
                    var detectAction = function () {
                        $(document).on("mouseover", mouseoverHandler);
                        pauseHeartBeat();
                        setTimeout(detectAction, 15 * 60 * 1000)
                    };
                    startHeartBeat();
                    visibly.onVisible(startHeartBeat);
                    visibly.onHidden(pauseHeartBeat);
                    setTimeout(detectAction, 15 * 60 * 1000)
                }
                $(document).on("click", function (event) {
                    if (event.originalEvent) {
                        var target = event.target;
                        var identity = identifyClickTarget(target);
                        if (identity.event == "in") {
                            _toZhangZhi(identity.event, identity.mod, {
                                url: identity.url,
                                recid: identity.recid,
                                topageid: identity.topageid,
                                liveid: identity.liveid,
                                recSrc: identity.recSrc
                            })
                        } else {
                            if (identity.mod) {
                                _toZhangZhi(identity.event, identity.mod, {
                                    url: identity.url,
                                    recid: identity.recid
                                })
                            }
                        }
                        if (isIE && identity.event == "in" && loc.hostname == "www.6.cn" && identity.url.indexOf(ZHANG_ZHI_QUERY_NAME) < 0 && identity.parentAnchor) {
                            var tracingCode = makeTracingCode(pageName, identity.mod) + "|" + (pageMessage._pageid || "") + "|" + (identity.recid || "") + "|" + (identity.liveid || "") + "|" + (identity.recSrc || "");
                            var url = identity.url;
                            var explodeByHash = url.split("#");
                            var hash = explodeByHash[1] ? "#" + explodeByHash[1] : "";
                            var urlWithoutHash = explodeByHash[0];
                            url += (urlWithoutHash.indexOf("?") > -1 ? "&" : "?") + ZHANG_ZHI_QUERY_NAME + "=" + tracingCode + hash;
                            identity.parentAnchor.href = url
                        }
                    }
                });
                setTimeout(function () {
                    _toZhangZhi("load", loadModule, {
                        url: loc.href
                    })
                }, 500);
                if (loc.search.indexOf(ZHANG_ZHI_QUERY_NAME + "=") > -1) {
                    var pattern = new RegExp(ZHANG_ZHI_QUERY_NAME + "\\=(.*)(?:$|\\&)");
                    var matchObj = pattern.exec(loc.search);
                    var tracingCookie = matchObj ? matchObj[1].split("|") : [];
                    pageMessage._tracing = tracingCookie[0] || "";
                    pageMessage._from_pageid = tracingCookie[1] || "";
                    pageMessage._from_recid = tracingCookie[2] || "";
                    pageMessage._from_liveid = tracingCookie[3] || "";
                    pageMessage._from_recSrc = tracingCookie[4] || ""
                } else {
                    var tracingCookie = (cookie.get(ZHANG_ZHI_COOKIE_NAME) || "").split("|");
                    pageMessage._tracing = tracingCookie[0] || "";
                    pageMessage._from_pageid = tracingCookie[1] || "";
                    pageMessage._from_recid = tracingCookie[2] || "";
                    pageMessage._from_liveid = tracingCookie[3] || "";
                    pageMessage._from_recSrc = tracingCookie[4] || ""
                }
                cookie.remove(ZHANG_ZHI_COOKIE_NAME, {
                    domain: ZHANG_ZHI_COOKIE_DOMAIN
                });
                if (loc.search.indexOf(ZHANG_ZHI_QUERY_NAME + "=") == -1) {
                    $(window).on("beforeunload", function () {
                        if (!cookie.get(ZHANG_ZHI_COOKIE_NAME) && pageMessage._tracing) {
                            cookie.set(ZHANG_ZHI_COOKIE_NAME, pageMessage._tracing + "|" + pageMessage._from_pageid + "|" + pageMessage._from_recid + "|" + pageMessage._from_liveid + "|" + pageMessage._from_recSrc, {
                                domain: ZHANG_ZHI_COOKIE_DOMAIN
                            })
                        }
                    })
                }
            })
        }
    };
    window[LANCE_CALLBACK] = function (data) {
        cookie.set("shrek_uuid", data.u || "", {
            expires: new Date(new Date().getTime() + 30 * 24 * 60 * 60 * 1000)
        });
        var _reft = data.r || 0;
        var _reftCK = cookie.get("shrek_reft");
        if (_reftCK) {
            _reftCK = _reftCK.split("|");
            if (_reftCK[1] == data.t) {
                _reft = _reftCK[0]
            }
        }
        cookie.set("shrek_reft", _reft + "|" + data.t, {
            domain: "6.cn",
            expires: new Date(new Date().getTime() + 30 * 24 * 60 * 60 * 1000)
        });
        try {
            var shrekStr = cookie.get("shrek") || "";
            var shrekAr = shrekStr.split("|");
            if (data.u && data.t && data.r != undefined && (!shrekStr || shrekAr[1] != data.t)) {
                cookie.set("shrek", data.u + "|" + data.t + "|" + data.r, {
                    domain: "6.cn"
                })
            }
        } catch (e) {}
        via.uuid = data.u || 0;
        via.triggerReady();
        window._UUID = via.uuid;
        window._REFT = _reft;
        setTimeout(function () {
            establishZhangZhiAnalytic(data.u || "", _reft)
        }, 100);
        if (data.o) {
            cookie.set("_apkReft", data.o, {
                path: "/event/liveClient"
            })
        }
    };
    toLance()
})();